//
//  BMSearchAnimationTool.m
//  bigmama
//
//  Created by huyuchen on 16/10/13.
//  Copyright © 2016年 . All rights reserved.
//

#import "BMSearchAnimationTool.h"
#import "UIView+YJFrame.h"

@interface BMSearchAnimationTool ()
@property (nonatomic, strong) NSObject<UIViewControllerContextTransitioning> *transitionContext;
@end

@implementation BMSearchAnimationTool

#pragma mark - 新方法
+ (instancetype)transition{
    BMSearchAnimationTool *transition = [[BMSearchAnimationTool alloc] init];
    return transition;
}

- (instancetype)init{
    if (self = [super init]) {
        //初始化为0
        self.backOffsetY = 0;
    }
    return self;
}

#pragma mark - 转场动画代理<必须实现>
// 持续时间
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.3f;
}

// 实现方式
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    if (_transitionType == BMSearchTransitionTypeShow) {
        [self showOffsetTransitionAnimation:transitionContext];
    }else if (_transitionType == BMSearchTransitionTypeHide){
        [self hideOffsetTransitionAnimation:transitionContext];
    }
    
}


#pragma mark - 动画实现
// 偏移向上
- (void)showOffsetTransitionAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIView *containerView = transitionContext.containerView;
    
    UIView *fromView = nil;
    UIView *toView = nil;
    
    if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
        //iOS8后
        fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
        toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    }else{
        fromView = fromVC.view;
        toView = toVC.view;
    }
    
    fromView.frame = [transitionContext initialFrameForViewController:fromVC];
    toView.frame = [transitionContext finalFrameForViewController:toVC];
    
    fromView.alpha = 1.0f;
    toVC.navigationController.navigationBar.alpha = 0;
    toView.alpha = 0;
    
    [containerView addSubview:toView];
    
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    
    [UIView animateWithDuration:duration animations:^{
        fromView.alpha = 0.0f;
        toVC.navigationController.navigationBar.alpha = 1;
        fromView.y = -40;
        toView.alpha = 1;
    } completion:^(BOOL finished) {
        BOOL wasCancelled = [transitionContext transitionWasCancelled];
        [transitionContext completeTransition:!wasCancelled];
    }];
}


// 偏移乡下
- (void)hideOffsetTransitionAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIView *containerView = transitionContext.containerView;
    
    UIView *fromView = nil;
    UIView *toView = nil;
    
    if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
        //iOS8后
        fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
        toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    }else{
        fromView = fromVC.view;
        toView = toVC.view;
    }
    
    fromView.frame = [transitionContext initialFrameForViewController:fromVC];
    toView.frame = [transitionContext finalFrameForViewController:toVC];
    
    fromView.alpha = 1.0f;
    toView.alpha = 0.0f;
    
    [containerView addSubview:toView];
    
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    
    toView.y = -40;
    [UIView animateWithDuration:duration animations:^{
        fromView.alpha = 0.0f;
        toView.alpha = 1.0f;
        toView.y = self.backOffsetY;
    } completion:^(BOOL finished) {
        BOOL wasCancelled = [transitionContext transitionWasCancelled];
        [transitionContext completeTransition:!wasCancelled];
    }];
}

@end
